package com.guit.hrms.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.guit.hrms.mapper.LoginMapper;
import com.guit.hrms.po.Employee;
import com.guit.hrms.po.WeChatSession;
import com.guit.hrms.service.LoginService;
import com.guit.hrms.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@Service
public class LoginServiceImpl implements LoginService {
    @Value("${wechat.app-id}")
    private String APP_ID;
    @Value("${wechat.app-secret}")
    private String APP_SECRET;
    @Value("${wechat.api-url}")
    private String WECHAT_API_URL;

    @Autowired
    private LoginMapper loginMapper;

    /**
     * 登录
     * @param employeeId 员工号
     * @param password 密码
     * @return 结果与员工信息
     */
    @Override
    public Employee login(String employeeId, String password) {
        String md5Password = MD5Util.encrypt(password);

        return loginMapper.Login(employeeId,md5Password);
    }

    /**
     * 获取微信session(微信登录)
     * @param code 微信code
     * @return 微信session
     */
    public WeChatSession getWeChatSession(String code) {
        RestTemplate restTemplate = new RestTemplate();
        String url = String.format("%s?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",
                WECHAT_API_URL, APP_ID, APP_SECRET, code);

        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

        if (response.getStatusCode() == HttpStatus.OK) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.readValue(response.getBody(), WeChatSession.class);
            } catch (IOException e) {
                throw new RuntimeException("Failed to parse WeChat session response", e);
            }
        } else {
            throw new RuntimeException("Failed to get WeChat session, status code: " + response.getStatusCode());
        }
    }

    /**
     * 绑定微信
     * @param employeeId 员工id
     * @param openid 微信openid
     * @return 绑定结果
     */
    @Override
    public Integer bindWeChat(String employeeId, String openid) {
//        String openId = null;
//        try {
//            openId = AESUtil.encrypt(openid);
//        } catch (Exception e) {
//            throw new RuntimeException("openId 加密出错");
//        }
        return loginMapper.bindWeChat(employeeId, openid);
    }

    @Override
    public Employee wxLogin(String employeeId, String password, String openid) {
        String md5Password = MD5Util.encrypt(password);

        Employee employee = loginMapper.Login(employeeId, md5Password);
        if (employee.getOpenId() != null){
//            String openId = null;
//            try {
//                openId = AESUtil.encrypt(openid);
//            } catch (Exception e) {
//                throw new RuntimeException("openId 加密出错");
//            }
            employee = loginMapper.wxLogin(employeeId,md5Password,openid);
        }

        return employee;
    }
}
